<template>
  <BasicDrawer 
    v-bind="$attrs" 
    @register="registerDrawer" 
    title="租户套餐" 
    :width="width" 
    @ok="handleSubmit" 
    destroyOnClose
  >
    <MyAlert type="success" message="当租户套餐发生变更时, 会异步更新关联的租户, 所以需要耐心等待几分钟, 租户绑定的权限套餐才会生效"/>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <!--菜单授权抽屉-->
    <TenantPackageDrawer @register="registerTenantPackageDrawer" @success="reload"/>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, useAttrs } from 'vue';
import MyAlert from '/@/components/My/MyAlert.vue';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import TenantPackageDrawer from './package/TenantPackageDrawer.vue';
import { useGlobSetting } from '/@/hooks/setting';
import { columns, searchFormSchema } from './package/data';
import { pageTenantPackage, deleteTenantPackage, batchDeleteTenantPackage } from './package/api';
import { useListPage } from '/@/hooks/system/useListPage';
import { useMessage } from '/@/hooks/web/useMessage';
import { Icon } from '/@/components/Icon';

// 声明Emits
const emit = defineEmits(['success', 'register']);
const attrs = useAttrs()

const tenantId = ref('')
const width = ref<number | string>('850px')
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'tenant-package',
  tableProps: {
    title: '租户套餐',
    api: pageTenantPackage,
    showTableSetting: false,
    showIndexColumn: false,
    bordered: false,
    striped: false,
    immediate: true,
    columns: columns,
    formConfig: {
      baseColProps: { 
        xs: 12, // <576px
        sm: 12, // ≥576px
        md: 12, // ≥768px
        lg: 12, // ≥992px
        xl: 12, // ≥1200px
        xxl: 12, // ≥1600px
      },
      wrapperCol: { sm: 18, md: 18 },
      showAdvancedButton: false,
      labelWidth: 80,
      showSubmitButton: false,
      showResetButton: false,
      schemas: searchFormSchema,
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
const { createMessage } = useMessage();
const glob = useGlobSetting();
const [registerTenantPackageDrawer, { openDrawer: openTenantPackageDrawer }] = useDrawer();

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  setDrawerProps({ confirmLoading: false });
});


/**
 * 提交
 */
async function handleSubmit() {
  try {
    closeDrawer();
    emit('success');
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}

/**
* 新增事件
*/
function handleAdd() {
  openTenantPackageDrawer(true, { isUpdate: false });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteTenantPackage({ id: record.id }, reload);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteTenantPackage({ ids: selectedRowKeys.value }, reload);
}

const handleEdit = (record) => {
  openTenantPackageDrawer(true, { isUpdate: true, record });
}

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '删除',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    }
  ]
}
</script>
